package com.fjycnet.qiniu;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.orangelife.R;
import com.qiniu.android.common.FixedZone;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UploadManager;

/**
 * 1 官方文档地址:https://developer.qiniu.com/kodo [2018年8月6日 星期一]
 * 2 SDK的Github地址:https://github.com/qiniu/android-sdk
 * 3 博客文章地址:暂时不需要 (放有道云笔记里,形成联动)
 * 4 常见问题:暂无
 * <p>
 * 2018年8月6日 星期一
 */
public class QiniuActivity extends AppCompatActivity {

    private UploadManager mQiNiuUploadManager;
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiniu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        initQiNiuSDK();
        tvResult = findViewById(R.id.tvResult);
        findViewById(R.id.btnUploadQn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                uploadFile();
            }
        });
    }

    private String getQiNiuToken() {
        //请自行向服务端获取,此处仅为测试
        return "rXPtjh0BFYCkwA7orCSzpdCK_ljMxHjF-wcALY9t:4_tdTD7sRhesB2koSipHPAbnzCk=:eyJzY29wZSI6IndlaWFpOndlaWFpIiwiZGVhZGxpbmUiOjE1MzM1NjA1MjV9";
    }

    private String getQiNiuDomain() {
        //请自行向服务端获取,此处仅为测试
        return "pbqyuu54g.bkt.clouddn.com";
    }

    private String getFileName() {
        //请自行获取本地图片地址做测试,这里为保持纯洁性,不会去耦合其他组件进来
        return "/storage/emulated/0/DCIM/Camera/1533181794110.jpg";
    }

    private void uploadFile() {
        String fileName = getFileName();
        String token = getQiNiuToken();
        String domain = getQiNiuDomain();
        YcQiNiuUtil.uploadSingle(tvResult, mQiNiuUploadManager, fileName, token, domain, new YcQiNiuUtil.MyQiniuListener() {
            @Override
            public void request(String url) {
                Log.e(QiniuActivity.class.getSimpleName(), "url=" + url);
            }
        });

    }

    //1 一般放在Application里初始化
    private void initQiNiuSDK() {
        mQiNiuUploadManager = new UploadManager(getQiNiuConfig());
    }

    //2 配置七牛
    private Configuration getQiNiuConfig() {
        return new Configuration.Builder()
                .chunkSize(512 * 1024)        // 分片上传时，每片的大小。 默认256K
                .putThreshhold(1024 * 1024)   // 启用分片上传阀值。默认512K
                .connectTimeout(10)           // 链接超时。默认10秒
                .useHttps(true)               // 是否使用https上传域名
                .responseTimeout(60)          // 服务器响应超时。默认60秒
                .zone(FixedZone.zone0)        // 设置区域，指定不同区域的上传域名、备用域名、备用IP。
                .build();
    }

}
